package com.yingxin.yx.framework.web.req;


import eu.bitwalker.useragentutils.DeviceType;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author fanchao
 */
@Slf4j
public class HttpHelper {

    private static final String UNKNOWN = "UNKNOWN";

    public static RequestInfo getRequestInfo() {
        return getRequestInfo(null);
    }

    public static RequestInfo getRequestInfo(HttpServletRequest request) {
        RequestInfo requestInfo = null;
        try {
            if (request == null) {
                request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            }
            Set<String> ips = new HashSet<>();
            String ip1 = request.getHeader("X-Real-IP");
            if (StringUtils.isNotBlank(ip1) && !UNKNOWN.equalsIgnoreCase(ip1) && !"127.0.0.1".equals(ip1)) {
                ips.add(ip1);
            }
            //if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            String ip2 = request.getHeader("X-Forwarded-For");
            if (StringUtils.isNotBlank(ip2) && !UNKNOWN.equalsIgnoreCase(ip2) && !"127.0.0.1".equals(ip2)) {
                ips.add(ip2);
            }
            //}
            //if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            String ip3 = request.getHeader("Proxy-Client-IP");
            if (StringUtils.isNotBlank(ip3) && !UNKNOWN.equalsIgnoreCase(ip3) && !"127.0.0.1".equals(ip3)) {
                ips.add(ip3);
            }
            //}
            //if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            String ip4 = request.getHeader("WL-Proxy-Client-IP");
            if (StringUtils.isNotBlank(ip4) && !UNKNOWN.equalsIgnoreCase(ip4) && !"127.0.0.1".equals(ip4)) {
                ips.add(ip4);
            }
            //}
            //if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            String ip5 = request.getHeader("HTTP_CLIENT_IP");
            if (StringUtils.isNotBlank(ip5) && !UNKNOWN.equalsIgnoreCase(ip5) && !"127.0.0.1".equals(ip5)) {
                ips.add(ip5);
            }
            //}
            //if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            String ip6 = request.getHeader("HTTP_X_FORWARDED_FOR");
            if (StringUtils.isNotBlank(ip6) && !UNKNOWN.equalsIgnoreCase(ip6) && !"127.0.0.1".equals(ip6)) {
                ips.add(ip6);
            }
            //}
            //if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            String ip7 = request.getRemoteAddr();
            if (StringUtils.isNotBlank(ip7) && !UNKNOWN.equalsIgnoreCase(ip7) && !"127.0.0.1".equals(ip7)) {
                ips.add(ip7);
            }
            //}
            requestInfo = new RequestInfo();
            requestInfo.setRemoteAddr(String.join(",", ips));
            requestInfo.setRequestURI(request.getRequestURI());
            requestInfo.setRequestMethod(request.getMethod());
            requestInfo.setUserAgent(request.getHeader("user-agent"));
            DeviceType deviceType = UserAgent.parseUserAgentString(requestInfo.getUserAgent()).getOperatingSystem().getDeviceType();
            requestInfo.setDeviceType(deviceType.getName());
            if (requestInfo.getUserAgent().toLowerCase().contains("micromessenger")) {
                requestInfo.setDeviceType("wechat");
            }
            Map<String, String[]> parameterMap = request.getParameterMap();
            requestInfo.setParameterMap(parameterMap);
        } catch (Exception e) {
            log.info(e.getMessage());
        }
        return requestInfo;
    }

    public static String getDeviceType() {
        return getDeviceType(null);
    }

    public static String getDeviceType(HttpServletRequest request) {
        if (request == null) {
            request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        }
        String userAgent = request.getHeader("user-agent");
        DeviceType deviceType = UserAgent.parseUserAgentString(userAgent).getOperatingSystem().getDeviceType();
        if (userAgent.toLowerCase().contains("micromessenger")) {
            return "wechat";
        }
        return deviceType.getName();
    }

}
